﻿using PLC.Domain.Dto;
using PLC.Domain.Enum;
using PLC.Domain.Models;

namespace PLC.Domain.ScheduledTasks.Strategy.Trigger;

public class ValidateMembraneElectrodeCodeStrategy : BaseTrigger
{
    public override EnumTrigger EnumTrigger { get; protected set; } = EnumTrigger.校验膜电极条码;
    public override int TriggerEvent(PLCConfig plcConfig, DtoDataConfig trigger, PLCReadModel plcReadModel)
    {
        // 如果触发的是等待   那么直接写入等待
        if (plcReadModel.TriggerValue == "1")
        {
            ValidFalseCode(plcReadModel.BarcodeDatas);
            return (int)EnumWriteType.等待;
        }

        var menbtrances = plcReadModel.BarcodeDatas.Where(p => !string.IsNullOrWhiteSpace(p.MembraneElectrode));
        if (menbtrances.Any()&&menbtrances.All(p=>p.MembraneElectrode.Length>=(trigger.CodeLength??0)))
        {
            // 数据最后统一保存
            return (int)EnumWriteType.OK;
        }
        // 校验失败做个标记  后面统一存数据库的时候 剔除掉
        ValidFalseCode(plcReadModel.BarcodeDatas);
        return (int)EnumWriteType.NG;
    }
    private void ValidFalseCode(List<DtoBarcodeData> barcodeDatas)
    {
        foreach (var barcodeData in barcodeDatas)
        {
            barcodeData.SetValidResult(false);
        }
    }
}